package com.zhouxiaoge.config.controller;

import com.zhouxiaoge.config.MyRowMapper;
import com.zhouxiaoge.config.entity.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class IndexController {


    private final JdbcTemplate jdbcTemplate;

    public IndexController(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @GetMapping
    public List<Map<String, Object>> index() {
        String sql = "SELECT * from config_properties";
        return jdbcTemplate.queryForList(sql);
    }

    @GetMapping("/{id}")
    public Config getId(@PathVariable("id") String id) {
        String sql = "select * from config_properties where id = ? ";

//        Config config1 = jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
//            Config config = new Config();
//            config.setId(rs.getString("id"));
//            config.setKey1(rs.getString("key1"));
//            config.setValue1(rs.getString("value1"));
//            config.setApplication(rs.getString("application"));
//            config.setProfile(rs.getString("profile"));
//            config.setLabel(rs.getString("label"));
//            return config;
//        }, id);
//        System.out.println(Objects.requireNonNull(config1).toString());

        return jdbcTemplate.queryForObject(sql, new MyRowMapper(), id);
    }
}
